﻿namespace EyeOpen.Services.Services
{
	using System.Linq;
	using System.Web.Mvc;
	using EyeOpen.Mvc.Models;
	using EyeOpen.Services;

	public class FrontEndController : Controller
	{
		private readonly IWebSiteService webSiteService;

		public FrontEndController()
			: this(new WebSiteService())
		{
		}

		public FrontEndController(IWebSiteService webSiteService)
		{
			this.webSiteService = webSiteService;
		}

		#if !DEBUG
		[OutputCache(Duration = 120, VaryByHeader = "Host")]
		#endif
		public ActionResult Index(string language, string path)
		{
			var contentLocalizedList =
				webSiteService
					.GetContentLocalizedList();
				
			var contentLocalized =
				contentLocalizedList.Single(x => x.Path == path && x.Language.TwoLetter == (language ?? "en"));

			var pathViewModelList =
				contentLocalizedList
					.OrderBy(x => x.Order)
					.Select(x => new SectionViewModel { Id = x.Id, Text = x.Text, Title = x.Title, Path = x.Path, Language = x.Language.TwoLetter })
					.ToList();

			return View(new WebSiteViewModel(contentLocalized, pathViewModelList));
		}
	}
}